$(function() {
	//初始化
	Begin();
})

var demoFormKey = '';


setTimeout(function thisTree(){
	Jurisdiction();
},100);

function dateFormat(d) {
    var date = new Date(d);
    return date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
}

function resetAllInfo(){
	VanyAjax("/demo/demoForm/query").done(function(data) {
		if(data&&data["demoformkey"]&&data["demoformkey"]!=''){
			debugger;
			demoFormKey = data["demoformkey"];
			fromDemo.demoForm.name = data["name"];
			fromDemo.demoForm.industry = data["industry"];
			fromDemo.demoForm.remark = data["remark"];
			fromDemo.demoForm.remarkCT= data["remark"].lengths;
			fromDemo.demoForm.resource = data["resource"];
			if(data["delivery"]=="1"){
				fromDemo.demoForm.delivery = true;
			}else{
				fromDemo.demoForm.delivery = false;
			}
			if(data["type"]&&data["type"]!=""){
				if(data["type"].indexOf(",")>-1){
					var types = data["type"].split(",")
					fromDemo.demoForm.type =types;
				}else{
					var types =[];
					types.push(data["type"]);
					fromDemo.demoForm.type =types;
				}
			}
			fromDemo.demoForm.demoDate = data["demodate"];
		}
	}, false, this);
}

var fromDemo = new Vue({
	el: "#fromDemo",
	data: function() {
		//自定义验证，验证机构编码
		var name = function(rule, value, callback) {
			if(!value) {
				return callback(new Error('请输入名称!'));
			}
			return callback();
		};
		
		return {
			demoForm:{
				name:'',
				industry:'',
				demoDate: '',
				delivery:false,
				type:[],
				resource:'',
				remark:'',
				remarkCT: 0
			},
			rules: {
				name: [{
					required: true,
					validator: name,
					trigger: 'blur'
				}]
			},
			industry:[],
			dialogVisible: false,
		}
	},
	created: function() {
		VanyAjax("/base/dictionary/industry", {}).done(function(data) {
			for(var i = 0; i < data.length; i++) {
				this.industry.push({
					value: data[i].industryCode,
					label: data[i].industryName
				});
			}
		}, false, this);
		
	},
	methods: {
		descInput: function(inputName) {
			if(inputName == "remark") {
				this.demoForm.remarkCT = this.demoForm.remark.length;
			}
		},
		resetForm: function(formName) {
			this.$refs[formName].resetFields();
		},
		submitForm: function(formName) {
			this.dialogVisible = true;
		},
		tijiao:function(obj) {
			this.dialogVisible = false;
			var self = this;
			var params =self.demoForm;
			var types = "";
			for(var i=0;i<params.type.length;i++){
				if(i==0)types = params.type[i];
				else types=types+","+params.type[i]
			}
			params.type = types;
			var demoDate = dateFormat(params.demoDate);
			params.demoDate = demoDate;
			if(obj) {
				var url = "";
				if(demoFormKey==''){
					url = "/demo/demoForm/add";
				}else{
					url = "/demo/demoForm/update";
					params["demoformkey"] = demoFormKey;
				}
				VanyAjax(url, params).done(function(data) {
					resetAllInfo();
					//A_license = licenseImport.licenseForm.license;
				}, true, self);
			}
		},
		// 表格操作区域批量导入
		BatchImport: function() {
			tanchuDialog(); //弹出框位置
			BatchImport.batchImportVisible = true;
		}
	}
	
});

resetAllInfo();


//批量导入
var BatchImport = new Vue({
	el: "#BatchImport",
	data: function() {
		this.orgFilter = null;
		return {
			batchImportVisible: false,
			labelPosition: 'top',
			filename: false,
			form: {},
			ruleForm: {
				batch: '',
				state: 1,
				orgList: ""
			},
			orgList: []
		}
	},
	methods: {
		show: function() {
			this.batchImportVisible = true;
		},
		hide: function() {
			this.batchImportVisible = false;
		},
		
		handleSuccess: function(res, file) {
			this.$message.success(res.rspMsg);
			var self = this;
			BatchImport.$nextTick(function() {
				self.filename = false;
			});
		},
		handleChange: function(file, fileList) {
			//传参
			var choiceState;
			BatchImport.ruleForm.state === 1 ? choiceState = "COVER" : choiceState = "IGNORE";
			BatchImport.form = {
				token: sessionStorage.token,
				repeatDeal: choiceState,
				orgCode: BatchImport.ruleForm.orgList
			}
			if(fileList.length === 2) {
				fileList.splice(0, 1);
			}
			console.log(fileList);
			var isXLS = file.raw.type === 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
			var isLt2M = file.size / 1024 / 1024 < 2;
			if(!isXLS) {
				this.$message.error('仅支持Excel格式！');
				return;
			}
			if(!isLt2M) {
				this.$message.error('上传文件大小不能超过 2MB!');
				return;
			}
			this.filename = file.name;
		},
		submitForm: function(formName) {
			this.$refs.upload.submit();
			this.$refs.ruleForm.resetFields();
			BatchImport.batchImportVisible = false;
			BatchImport.orgList = [];
			BatchImport.ruleForm.state = 1;
		}
		//	},
		//	updated: function() {
		//		Jurisdiction();
	}
});
//初始化
function Begin() {
	//面包屑
	new Vue().$mount('#crumbs');
	//sessionStorage.Jurisdiction = ""
}